package cn.kgc.ssm.controller;

import cn.kgc.ssm.service.AppVersionService;
import cn.kgc.ssm.pojo.AppCategory;
import cn.kgc.ssm.pojo.AppInfo;
import cn.kgc.ssm.pojo.AppVersion;
import cn.kgc.ssm.pojo.DataDictionary;
import cn.kgc.ssm.service.AppCategoryService;
import cn.kgc.ssm.service.AppInfoService;
import cn.kgc.ssm.service.DataDictionaryService;


import cn.kgc.ssm.tools.Constants;
import com.github.pagehelper.PageHelper;


import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yxp
 * @create 2021-05-20 16:11
 */
@Controller
@RequestMapping("manager/backend/app")
public class AppCheckController {

    @Resource
    DataDictionaryService dataDictionaryService;

    @Resource
    AppCategoryService appCategoryService;

    @Resource
    AppInfoService appInfoService;

    @Resource
    AppVersionService appVersionService;


    @RequestMapping("list")
    public String list(Model model, @RequestParam(value = "querySoftwareName",required = false) String querySoftwareName,
                       @RequestParam(value = "queryFlatformId",required = false) Integer queryFlatformId,
                       @RequestParam(value = "queryCategoryLevel1",required = false) Integer queryCategoryLevel1,
                       @RequestParam(value = "queryCategoryLevel2",required = false) Integer queryCategoryLevel2,
                       @RequestParam(value = "queryCategoryLevel3",required = false) Integer queryCategoryLevel3,
                       @RequestParam(value="pageIndex",required = false, defaultValue = "1" )Integer pageIndex){

        PageHelper.startPage(pageIndex,5);
        List<AppInfo> appInfoList=appInfoService.getAppInfoList(querySoftwareName,queryFlatformId,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3);

        PageInfo<AppInfo> pageInfo=new PageInfo<>(appInfoList);
        //查询平台
        List<DataDictionary> dataDictionaryList=dataDictionaryService.getDataDictionaryList("APP_FLATFORM");
        //查询一级分类
        List<AppCategory> appCategoryList=appCategoryService.getAppCategoryLevel1List(null);




        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("flatFormList",dataDictionaryList);
        model.addAttribute("categoryLevel1List",appCategoryList);
//      model.addAttribute("appInfoList",appInfoList);
        model.addAttribute("querySoftwareName",querySoftwareName);
        model.addAttribute("queryFlatformId",queryFlatformId);
        model.addAttribute("queryCategoryLevel1",queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2",queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3",queryCategoryLevel3);


        //回响二级分类和三级分类
        List<AppCategory> appCategoryListLevel2=appCategoryService.getAppCategoryLevel1List(queryCategoryLevel1);
        List<AppCategory> appCategoryListLevel3=appCategoryService.getAppCategoryLevel1List(queryCategoryLevel2);

        System.out.println("学号"+appCategoryListLevel3);
        model.addAttribute("categoryLevel2List",appCategoryListLevel2);
        model.addAttribute("categoryLevel3List",appCategoryListLevel3);
        return "backend/applist";
    }

    @RequestMapping("categorylevellist")
    @ResponseBody
    public List<AppCategory> categorylevellist(Integer pid){
        List<AppCategory> appCategoryList=appCategoryService.getAppCategoryLevel1List(pid);
        return appCategoryList;
    }

    @RequestMapping("check")
    public String check(Integer aid,Integer vid,Model model){
       AppInfo appInfo=appInfoService.getAppInfoById(aid);
       AppVersion appVersion=appVersionService.getAppVersionById(vid);
       model.addAttribute("appInfo",appInfo);
       model.addAttribute("appVersion",appVersion);
       return "backend/appcheck";
    }

    @RequestMapping("checksave")
    public String checksave(Integer status,Integer id){
        appInfoService.updateStatusById(status,id);


        return "redirect:/manager/backend/app/list";
    }
}
